package com.ch.android.networklib

import android.content.Context
import com.ch.android.baselib.SpUtil
import com.ch.android.baselib.SpUtil.getUserId
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response

/**
 * Author: conghao
 * Date: 2023/12/28 15:01
 * Description: MyInterceptor
 */
class MyInterceptor(context: Context): Interceptor {
    private val TAG = "MyInterceptor"
    private val mContext: Context
    init {
        this.mContext = context.applicationContext
    }

    override fun intercept(chain: Interceptor.Chain): Response {
        val originalRequest = chain.request()
        return when (originalRequest.url.toUrl().toURI().path) {
            ApiService.USER_LOGIN_PATH,
            ApiService.WX_LOGIN_PATH,
            ApiService.REGISTER_PATH -> {
                chain.proceed(originalRequest)
            }
            else -> {
                val newRequest: Request = originalRequest.newBuilder()
                    .addHeader("userId", SpUtil.getSP().getUserId().toString())
                    .build()
                chain.proceed(newRequest)
            }
        }
    }

}